package model;

public class Subject {
    //成员属性:学科名称/学科编号/学科年制/报名选修的学生信息/报名选修的学生个数
    private String subName;
    private String subNo;
    private int subLife;
    private Student[] myStudents;
    private int stuNum;
    //创建get/set/方法
    public String getSubName() {
        return subName;
    }

    public void setSubName(String subName) {
        this.subName = subName;
    }

    public String getSubNo() {
        return subNo;
    }

    public void setSubNo(String subNo) {
        this.subNo = subNo;
    }

    public int getSubLife() {
        return subLife;
    }
    //设置学制年限,限制必须>0
    public void setSubLife(int subLife) {
        if (subLife<=0){
            return;
        }
        this.subLife = subLife;
    }
    //获取选修专业的学生信息 如果保存学生信息的数组未被初始化,先初始化
    public Student[] getMyStudents() {
        if (this.myStudents==null){
            //初始化学生数组
            this.myStudents=new Student[200];
        }
        return myStudents;
    }

    public void setMyStudents(Student[] myStudents) {
        this.myStudents = myStudents;
    }

    public int getStuNum() {
        return stuNum;
    }

    public void setStuNum(int stuNum) {
        this.stuNum = stuNum;
    }

    //无参构造方法
    public Subject(){

    }
    //带参构造方法,实现对属性的部分赋值
    public Subject(String subName,String subNo,int subLife){
        //接收数据
        this.setSubName(subName);
        this.setSubNo(subNo);
        this.setSubLife(subLife);
    }
    //带参构造方法,实现对属性的全部赋值
    public Subject(String subName,String subNo,int subLife,Student[] myStudents){
        //接收数据
        this.setSubName(subName);
        this.setSubNo(subNo);
        this.setSubLife(subLife);
        this.setMyStudents(myStudents);
    }
    //专业介绍方法
    public String info(){
        String str="专业信息如下:\n专业名称:"+this.getSubName()+"\n专业的编号:"+this.getSubNo()+"\n学制年限:"+getSubLife()+"年";
        return str;
    }
    public void addStudent(Student stu){
        /*
        1.将学生保存到数组中
        2.将学生个数保存到stuNum中
         */
        //1.将学生保存到数组中
        for (int i=0;i<this.getMyStudents().length;i++){
            if (this.getMyStudents()[i]==null){
                //进行学生关联专业
                stu.setStuSubject(this);
                //进行专业关联学生
                this.getMyStudents()[i]=stu;
                //2.将学生个数保存到stuNum中
                this.setStuNum(i+1);
                return;
            }
        }

    }
}
